<template>
  <view class="subjectPage">
    <uni-nav-bar title="专 题 讲 座" leftIcon="icon-fanhui" rightIcon="true" @clickLeft="$back()">
    </uni-nav-bar>
 <!--   <view class="subject u-f u-f-ac">
      <text class="fontsize24">学科:</text> -->
      <radio-components :value.sync="subjectIndex" :range="list" rangekey="name"></radio-components>

    <!-- </view> -->
   

    <view class="list">
      <view class="list-item" v-for="(item,index) in list[subjectIndex].fileList" :key="index" @click="$toPage(`./play?title=${item.name}&videoUrl=${item.videoUrl}&fileUrl=${item.fileUrl}`)">
        <view class="item-img positionRe">
           <image :src="item.image" mode="aspectFill"></image>
           <image class="sanjiao" src="/static/img/sanjiao.png"></image>
           <span class="iconfont icon-bofang bofang"></span>
        </view>
       
        <view class="color64 words">
          {{item.name}}
        </view>
      </view>
    </view>



  </view>
</template>

<script>
  import radioComponents from '@/components/radio-components.vue'
  export default {
    components: {
      'radio-components': radioComponents
    },
    data() {
      return {
        subjects: [{
          disciplineTitle: '语文',
          id: 1
        }],
        // 科目列表
        subjectIndex: 0,
        // 专题列表
        specialList: [{
          id: 1,
          name: '语文',
          videoUrl: 'file:///storage/emulated/0/videoTopic/yuwen/16063818829960.mp4',
          fileUrl: '/storage/emulated/0/videoTopic/yuwen/ccf840a8ff894eb69944b7a107e088c3.pdf',
          image: 'file:///storage/emulated/0/videoTopic/yuwen/nothing.png'
        }],
        list: [{
          name: "请选择科目",fileList:[1,2,3]
        }],
        entries: []
      };
    },
    created() {
      this.getSpecialSubject('/storage/emulated/0/videoTopic')
    },
    methods: {
      // 获取专题列表
      getSpecialSubject(url) {
        // let id = this.subjects[this.subjectIndex].id
        // this.list = this.specialList.filter(item => item.id === id)
        plus.io.resolveLocalFileSystemURL(
          url,
          (entry) => {
            //获取文件目录及子文件信息
            entry.createReader().readEntries((res) => {
              this.list = []
              // console.log(res)
              //循环学科目录
              res.forEach((item) => {
                // let obj = {fileList:[]}
                // obj.name = item.name
                // let fileList = []
                item.createReader().readEntries((res1) => {

                  var dirSize = 0;

                  // for (var x = 0; x < res1.length; x++) {
                  //   let bl = res1[x].isFile
                  //   if (!bl) {
                  //     // dirSize++
                  //     console.log(++dirSize)
                  //     // disSize = dirSize + 1;
                  //   }
                  // }
                  // 循环学科下的视频目录
                  for (let i = 0; i < res1.length; i++) {
                    if (!res1[i].isFile) {
                      let obj_node = {}
                      obj_node.name = res1[i].name
                      res1[i].createReader().readEntries(async (res2) => {
                        // 循环视频目录下的文件
                        for (let j = 0; j < res2.length; j++) {
                          // 截取文件名后缀
                          let suffix=res2[j].name.split('.')[1]
                            if (['jpg','png','jpeg'].includes(suffix)) {
                              obj_node.image = encodeURI("file://" + res2[j].fullPath)
                            }
                            if (['mp4','flv','wmv','mov'].includes(suffix)) {
                              obj_node.videoUrl =encodeURI(res2[j].fullPath) 
                            }
                            if (['ppt','doc','pdf','xls', 'pptx','docx','xlsx'].includes(suffix)) {
                              obj_node.fileUrl =res2[j].fullPath
                            }
                              // console.log(obj_node)
                            if (j === res2.length - 1) {
                             
                              // 是否已存在学科
                              let index = this.list.findIndex(key => key.name === item.name)
                              if (index > -1) {
                                // 已存在叠加fileList
                                this.list[index].fileList.push(obj_node)
                              } else {
                                let sort=1;
                                switch(item.name){
                                  case '语文':sort=1;break;
                                  case '数学':sort=2;break;
                                  case '英语':sort=3;break;
                                  case '物理':sort=4;break;
                                  case '生化':sort=5;break;
                                  default:sort=6;break;
                                }
                                // 否则将学科和学科下的视频文件添加到list
                                this.list.push({
                                  sort:sort,
                                  name: item.name,
                                  fileList: [obj_node]
                                })
                              }
                              
                              this.list=this.list.sort(function(a,b){
                                return a.sort-b.sort
                              })
                              // console.log(this.list)
                            }
                        
                        }
                      })
                    }
                  }

                })
              })
              //此处相关知识请见 DirectoryEntry 文件系统中的目录对象，用于管理特定的本地目录
            })
          });
      }

    }
  }
</script>

<style lang="scss">
  .subjectPage {
    min-height: 100%;
    background-color: #FFFFFF;
    @mixin sanjiao {
        width:0px;
                  height:0px;
      
                  border-top:50px solid rgba(0,0,0,0);
                  border-right:50px solid  rgba(0,0,0,0);
                  border-bottom:50px solid green;
                  border-left:50px solid  rgba(0,0,0,0);
      
                  margin-bottom:10px;
    }
    .list {
      margin: px2vw(32) 0px 0px px2vw(32);

      &-item {
        @include list-item
      }
    }
  }
</style>
